package com.ideafactory.client.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

/**
 * Created by LiuShao on 2016/2/21.
 */
public class SpUtils {

    private static SharedPreferences sp;
    public  static final String SP_NAME = "config";
    public  static final String  DEVICENO= "deviceNo"; //设备编号
    public  static final String  STOREID= "storeId";//店铺Id
    public  static final String  MODELID= "modelId";//模版id
    public  static final String  ADVERTID= "advertId";//广告id
    public  static final String  PRINTID= "printId";//小票模版id
    public static final String SHOP_IMG = "shopImg";//商店图片
    public static final String SHOP_NAME = "branchShop";//商店名称
    public static final String SHOP_ADDRESS = "shopAdress";//商店地址
    public static final String ISONLINEPAY = "isONlinePay";//是否支持扫码,默认都支持
    public static final String ISOFFLINEPAY = "isOfflinePay";//是否支持扫描枪，1是支持，2是不支持
    public static final String EATIN = "eatin";//是否支持堂食，1是支持，2是不支持
    public static final String PACKAGEING = "packageing";//是否支持打包，1是支持，2是不支持
    public static final String PACKING_MONEY ="packing_money";//打包的钱
    public static final String ISALIPAY ="isAlipay";//支付宝支付
    public static final String ISWXPAY ="isWxPay";//微信支付

    public static final String ISPRINT ="isprint";//是否打印
    public static final String EXITPASSWORD ="deviceExitPassword";//退出程序
    public static final String WIFI_IP ="wifi_ip";//wifi打印机ip
    public static final String WIFI_PORT ="wifi_port";//wifi打印机port
    public static final String NUMBER ="NUMBER";//
    public static final String LANGUAGUE="language";


    public static String[] getSharedPreference(String key) {
        String regularEx = "#";
        String[] str = null;
        String values;
        values = sp.getString(key, "");
        str = values.split(regularEx);

        return str;
    }

    public static void setSharedPreference(String key, String[] values) {
        String regularEx = "#";
        String str = "";

        if (values != null && values.length > 0) {
            for (String value : values) {
                str += value;
                str += regularEx;
            }

            sp.edit().putString(key, str).commit();
        }
    }

    public static void saveString(Context context,String key, String value){
        if(sp==null)
            sp = context.getSharedPreferences(SP_NAME,0);
        sp.edit().putString(key, value).commit();
    }

    public static String getString(Context context,String key,String defValue){
        if(sp==null)
            sp=context.getSharedPreferences(SP_NAME,0);
        return sp.getString(key, defValue);
    }

    public static void saveInt(Context context,String key, int value){
        if(sp==null)
            sp = context.getSharedPreferences(SP_NAME,0);
        sp.edit().putInt(key, value).commit();
    }

    public static int getInt(Context context,String key,int value){
        if(sp==null)
            sp=context.getSharedPreferences(SP_NAME,0);
        return sp.getInt(key,value);
    }
    public static void saveBoolean(Context context,String key,boolean value){
        if(sp == null)
            sp = context.getSharedPreferences(SP_NAME,0);
        sp.edit().putBoolean(key, value).commit();
    }

    public static boolean getBoolean(Context context , String key,boolean defValue){
        if(sp == null)
            sp = context.getSharedPreferences(SP_NAME,0);
        return sp.getBoolean(key, defValue);
    }


    public static String SceneList2String(List SceneList) {
        try {
            // 实例化一个ByteArrayOutputStream对象，用来装载压缩后的字节文件。
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            // 然后将得到的字符数据装载到ObjectOutputStream
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            // writeObject 方法负责写入特定类的对象的状态，以便相应的 readObject 方法可以还原它
            objectOutputStream.writeObject(SceneList);
            // 最后，用Base64.encode将字节文件转换成Base64编码保存在String中
            String SceneListString = new String(Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
            // 关闭objectOutputStream
            objectOutputStream.close();

            return SceneListString;

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static List String2SceneList(String SceneListString){
        try {
            byte[] mobileBytes = Base64.decode(SceneListString.getBytes(), Base64.DEFAULT);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            List SceneList = (List) objectInputStream.readObject();
            objectInputStream.close();
            return SceneList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
